<?php
include('../../../../inc/application_settings.php');

/**
 * saves content from editor to file
 * @param 		string			 $file
 * @param 		string			 $content
 * @param 		array			 $response
 */
function saveContent($file, $content, &$response) {
	try {
		$fp = fopen('../../../website/' . $file, 'w');
		flock($fp, 2);
		$content = str_replace("\\", "", $content);
		fputs($fp, $content);
		flock($fp, 3);
		fclose($fp);
	} catch (Exception $e) {
		$response['SUCCESS'] = 0;
		$response['ERROR_MSG'] = $e->getMessage();
	}
}

$file = (isset($_REQUEST['FILE'])) ? $_REQUEST['FILE'] : null;
$content = (isset($_REQUEST['CONTENT'])) ? $_REQUEST['CONTENT'] : null;
$response = array(
		'SUCCESS' => 1);

$email = $_COOKIE['EMAIL'];
$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);

if ($auth->checkAuthentication()) {
	if ($file != null) {
		saveContent($file, $content, $response);
	}
} else {
	$response['SUCCESS'] = 0;
	$response['ERROR_MSG'] = 'Authentication failed';
}

echo json_encode($response);
?>